#region Summary

// 
// Owner : JackieHan 
// 
// Solution : UpdateSystem
// 
// Project :Project
// 
// File : PublishLogInfo.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 发布信息
// 
// History : 
//			2011-01-05 10:26:30	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml;
using Jackie.Utility;

#endregion

namespace Jackie.Update.Entities.Server
{
    /// <summary> 
    /// 发布信息
    /// </summary>
    [DataContract(Name = "publishLog", Namespace = "http://serializer.skomart.net/publishLog")]
    [KnownType(typeof(UpdateFileInfo))]
    public class PublishLogInfo
    {
        public PublishLogInfo(XmlNode node)
        {
            var publishElement = node as XmlElement;
            if (publishElement == null)
                throw new FormatException("xmlNode to XmlElement Error");

            var fileNodes = publishElement.GetElementsByTagName("File");
            var files = (from XmlNode fileNode in fileNodes
                         select new UpdateFileInfo(fileNode)).ToList();

            var descriptionNodes = publishElement.GetElementsByTagName("Description");
            var descriptions = (from XmlNode descriptionNode in descriptionNodes
                                select descriptionNode.InnerText).ToList();

            Date = JackieConvertor.ToDataTimeThrowException(node.Attributes["date"].Value);
            Descriptions = descriptions;
            Files = files;
            SystemVersion = node.Attributes["systemVersion"].Value;
        }

        public PublishLogInfo()
        {
        }

        /// <summary>
        /// 发布日期
        /// </summary>
        [DataMember]
        public DateTime Date { get; set; }
        /// <summary>
        /// 系统版本号
        /// </summary>
        [DataMember]
        public string SystemVersion { get; set; }
        /// <summary>
        /// 更新文件
        /// </summary>
        [DataMember]
        public IList<UpdateFileInfo> Files { get; set; }
        /// <summary>
        /// 更新说明
        /// </summary>
        [DataMember]
        public IList<string> Descriptions { get; set; }

        public XmlNode CreateConfigNode(XmlDocument doc)
        {
            var publishEleme = doc.CreateElement("Publish");

            var dateAttr = doc.CreateAttribute("date");
            dateAttr.Value = Date.ToString();
            publishEleme.Attributes.Append(dateAttr);

            var versionAttr = doc.CreateAttribute("systemVersion");
            versionAttr.Value = SystemVersion;
            publishEleme.Attributes.Append(versionAttr);

            var updateFilesElement = doc.CreateElement("UpdateFiles");
            foreach (var file in Files)
            {
                var fileElement = file.CreateConfigNode(doc);
                updateFilesElement.AppendChild(fileElement);
            }
            publishEleme.AppendChild(updateFilesElement);

            var descriptionsElement = doc.CreateElement("Descriptions");
            foreach (var description in Descriptions)
            {
                var descriptionElement = doc.CreateElement("Description");
                descriptionElement.InnerText = description;
                descriptionsElement.AppendChild(descriptionElement);
            }
            publishEleme.AppendChild(descriptionsElement);
            return publishEleme;
        }
    }
}